package com.habdqn.attendance.dao.impl;

import com.habdqn.attendance.dao.BaseDao;
import com.habdqn.attendance.dao.DepartmentDao;
import com.habdqn.attendance.entity.Department;
import com.habdqn.attendance.entity.example.DepartmentExample;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author XingJiejian
 * @date 2019/10/24 0024
 */
public class DepartmentDaoImpl extends BaseDao implements DepartmentDao {
    private String selectSql = "SELECT id,name FROM department";

    @Override
    public Department queryById(Integer id) {
        String sql = selectSql + " WHERE id=?";
        List<Department> depts = this.getListByResultSet(this.executeQuery(sql, id));
        if (depts != null && depts.size() > 0) {
            return depts.get(0);
        }
        return null;
    }

    @Override
    public List<Department> queryAllByLimit(int offset, int limit) {
        String sql = selectSql + " LIMIT ?, ?";
        return this.getListByResultSet(this.executeQuery(sql, offset, limit));
    }

    @Override
    public List<Department> queryAllByLimit(String likeName, int offset, int limit) {
        String sql = selectSql + " WHERE name like ? LIMIT ?, ?";
        return this.getListByResultSet(this.executeQuery(sql, "%" + likeName + "%", offset, limit));
    }

    @Override
    public List<Department> queryAll(DepartmentExample department) {
        StringBuilder sql = new StringBuilder(selectSql);
        if (department != null) {
            sql.append(" WHERE 1=1 ");
            List<Object> paramList = new ArrayList<>();
            if (department.getId() != null) {
                sql.append(" AND id=? ");
                paramList.add(department.getId());
            } else if (department.getIds() != null && !"".equals(department.getIds())) {
                sql.append(" AND id IN(").append(department.getIds()).append(")");
            }
            if (department.getName() != null && !"".equals(department.getName())) {
                sql.append(" AND name LIKE ?");
                paramList.add("%" + department.getName() + "%");
            }
            // 组装分页条件
            if (department.getLimit() != null) {
                sql.append(" LIMIT ?");
                if (department.getOffset() != null) {
                    sql.append(" ,? ");
                    paramList.add(department.getOffset());
                }
                paramList.add(department.getLimit());
            }
            // 组装排序条件
            if (department.getOrderByClause() != null && !"".equals(department.getOrderByClause())) {
                sql.append(" ORDER BY ").append(department.getOrderByClause());
            }
            return this.getListByResultSet(this.executeQuery(sql.toString(), paramList.toArray()));
        }
        return this.getListByResultSet(this.executeQuery(sql.toString()));
    }

    @Override
    public int insert(Department department) {
        String sql = "INSERT INTO department(name) VALUES(?)";
        return this.executeInsert(sql, department.getName());
    }

    @Override
    public void insert(List<Department> depts) {
        String sql = "INSERT INTO department(name) VALUES(?)";
        List<Object[]> paramsList = new ArrayList<>();
        for (Department dept : depts) {
            Object[] params = {dept.getName()};
            paramsList.add(params);
        }
        this.executeBatch(sql, paramsList);
    }

    @Override
    public int update(Department department) {
        String sql = "UPDATE department SET name=? WHERE id=?";
        return this.executeUpdate(sql, department.getName(), department.getId());
    }

    @Override
    public int deleteById(Integer id) {
        String sql = "DELETE FROM department WHERE id=?";
        return this.executeUpdate(sql, id);
    }

    @Override
    public int batchDeleteByIds(String ids) {
        String sql = "DELETE FROM department WHERE id IN(" + ids + ")";
        return this.executeUpdate(sql);
    }

    @Override
    public int getAllCount() {
        String sql = "SELECT COUNT(*) FROM department";
        return (int) (long) this.uniqueResult(sql);
    }

    @Override
    public int getAllCount(String likeName) {
        String sql = "SELECT COUNT(*) FROM department WHERE name like ?";
        return (int) (long) this.uniqueResult(sql, "%" + likeName + "%");
    }


    private List<Department> getListByResultSet(ResultSet rs) {
        List<Department> depts = null;
        if (rs != null) {
            depts = new ArrayList<>();
            try {
                while (rs.next()) {
                    Department dept = new Department();
                    dept.setId(rs.getInt("id"));
                    dept.setName(rs.getString("name"));
                    depts.add(dept);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeAll();
            }
        }
        return depts;
    }
}
